package com.yt.file.service;

import com.yt.file.common.utils.FastDFSClient;
import com.yt.file.models.service.IFileService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件服务实现类（仅负责文件上传）
 */
@Service
@DubboService
public class FileServiceImpl implements IFileService {
    private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Autowired
    private FastDFSClient fastDFSClient;

    // 移除工单服务依赖：删除 @DubboReference private IWorkOrderInfoService workOrderInfoService;

    @Override
    public String uploadImage(MultipartFile file) {
        // 检查文件是否为空
        if (file == null || file.isEmpty()) {
            logger.error("上传文件为空");
            throw new RuntimeException("上传文件为空");
        }

        // 检查文件类型是否为图片
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            logger.error("上传文件不是图片类型");
            throw new RuntimeException("上传文件不是图片类型");
        }

        // 上传文件到FastDFS并返回地址
        String fileId = fastDFSClient.uploadFile(file);
        return fileId;
    }

    @Override
    public List<String> uploadImages(List<MultipartFile> files) {
        // 检查文件列表是否为空
        if (files == null || files.isEmpty()) {
            logger.error("上传文件列表为空");
            throw new RuntimeException("上传文件列表为空");
        }

        List<String> imageUrls = new ArrayList<>();
        for (MultipartFile file : files) {
            imageUrls.add(uploadImage(file)); // 复用单文件上传逻辑
        }
        return imageUrls;
    }

    // 移除与工单关联的方法：删除 uploadImageAndLinkToWorkOrder 和 uploadImagesAndLinkToWorkOrder
}